<template>
  <div class="app-container">
    <el-table :height="tableHeight" v-loading="loading" :data="ruleList" stripe border>
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="规则名称" align="center" prop="ruleName" />
      <el-table-column label="规则数值" align="center" prop="ruleValue" :show-overflow-tooltip="true" />
      <el-table-column label="说明" align="center" prop="paramDesc" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="configType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.operation_rule_status" :value="scope.row.useStatus" />
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['operation:rule:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规则名称" prop="configName">
          <el-input v-model="form.ruleName" :disabled="true" />
        </el-form-item>
        <el-form-item label="规则数值" prop="configKey">
          <el-input v-model="form.ruleValue" placeholder="请输入数值" />
        </el-form-item>

        <el-form-item label="说明" prop="paramDesc">
          <el-input v-model="form.paramDesc" type="textarea" :rows="2" placeholder="请输入规则" />

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRule, editRule } from "@/api/operation/rule";

export default {
  name: "Config",
  dicts: ["operation_rule_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 参数表格数据
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ruleValue: [
          { required: true, message: "规则数值不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 180px)'
    },
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then((response) => {
        this.ruleList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paramCode: undefined,
        paramValue: undefined,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.paramCode = row.paramCode;
      this.open = true;
      this.title = "修改规则";
      this.form = Object.assign({}, row);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          editRule(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
